package network;

/**
 * represents a discrete data element to be sent
 * @author jack
 *
 */
public final class Data
{
	private byte interpreter;
	private byte[] data;
	private long creationTime;
	private long expirationTime = -1;
	
	/**
	 * creates a new packet of data with no expiration time
	 * @param interpreter the interpreter to use when decoding and interpreting
	 * the information once received
	 * @param data
	 */
	public Data(byte interpreter, byte[] data)
	{
		this.interpreter = interpreter;
		this.data = data;
	}
	/**
	 * creates a time sensitive data element, if the network profile handling this data
	 * discovers that the data is expired then it will not be sent to clients, time sensitive
	 * data may be sent either reliably or unreliably
	 * 
	 * note: the expiration time will not be sent, hence if a client receives this data element
	 * it will assume its relevant even if it may be expired
	 * @param interpreter
	 * @param data
	 * @param expirationTime the expiration time in ms
	 */
	public Data(byte interpreter, byte[] data, long expirationTime)
	{
		this(interpreter, data);
		creationTime = System.currentTimeMillis();
		this.expirationTime = expirationTime;
	}
	public boolean isExpired()
	{
		return System.currentTimeMillis()-creationTime >= expirationTime;
	}
	public byte[] getData()
	{
		return data;
	}
	public byte getInterpreter()
	{
		return interpreter;
	}
}
